<template>
  <el-dialog :title="title" custom-class="warn-dialog" :close-on-click-modal="false" :modal="false" :visible.sync="open"
    :before-close="handleClose">
    <!-- 查询条件 -->
    <el-form ref="queryForm" :model="query" size="small" class="query-form" inline>
      <el-form-item label="物资名称" prop="suppliesName">
        <el-input v-model="query.suppliesName" placeholder="请输入物资名称" @keyup.enter.native="getList"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="mini" @click="handleReset">重置</el-button>
        <el-button size="mini" @click="getList">查询</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格数据 -->
    <el-table :data="tableData" cell-class-name="table-cell-custom" header-cell-class-name="table-herder-custom"
      highlight-current-row :cell-style="cellStyle" @row-dblclick="dblclick" height="calc(100% - 3.75vw)" size="small">
      <el-table-column label="时间" prop="createTime" align="center" :formatter="formatDate" min-width="150">
      </el-table-column>
      <el-table-column label="物资名称" prop="suppliesName"> </el-table-column>
      <el-table-column label="关联设备">
        <template slot-scope="scope">
          {{associatedDevice(scope.row)}}
        </template>
      </el-table-column>
      <el-table-column label="SO2" prop="realInfo.First_airState" align="center" show-overflow-tooltip>
      </el-table-column>
      <el-table-column label="CH4" prop="realInfo.Second_airState" align="center" show-overflow-tooltip>
      </el-table-column>
      <el-table-column label="CO" prop="realInfo.Third_airState" align="center" show-overflow-tooltip></el-table-column>
      <el-table-column label="H2S" prop="realInfo.Fourth_airState" align="center" show-overflow-tooltip>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="query.current" :limit.sync="query.size" align="left"
      popperClass="default-select-popper" @pagination="getList" />
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCommit()" size="small">确 定</el-button>
      <el-button @click="handleClose" size="small">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { suppliesRealinfoWarning } from '@/api/supplies'
import Pagination from '@/components/Pagination'

export default {
  props: {
    open: Boolean,
  },
  components: {
    Pagination,
  },
  data() {
    return {
      title: '告警信息',
      total: 0, // 条数
      query: { current: 1, size: 20, suppliesName: '', isDanger: 1 },
      tableData: [],
      selectedRows: [],
    }
  },
  methods: {
    timeWidth() {
      return '20vw'
    },
    // 单元格样式
    cellStyle({ row, column, rowIndex, columnIndex }) {
      if (columnIndex > 2) {
        let props = column.property.split('.')
        if (row[props[0]][props[1]].includes('低报')) {
          return { color: '#ce8b2f' }
        }
        if (row[props[0]][props[1]].includes('高报')) {
          return { color: '#FF0000' }
        }
        return { color: '#68c23a' }
      }
    },
    // 日期格式化
    formatDate(row) {
      return row.createTime.substr(0, 19).replace('T', ' ')
    },
    // 关联设备
    associatedDevice(row) {
      let supplies = this.$store.state.cache.suppliesMap.get(row.suppliesId)
      if (supplies && supplies.reSuppliesId) {
        let video = this.$store.state.cache.videoMap.get(supplies.reSuppliesId)
        if (video) {
          return video.videoName
        }
      }
      return ''
    },
    // 双击
    dblclick(row) {},
    // 重置查询
    handleReset() {
      this.$refs.queryForm.resetFields()
      this.getList()
    },
    // 确定
    handleCommit() {
      this.handleClose()
      this.$emit('commit', this.selectedRows)
    },
    // 关闭或取消
    handleClose() {
      console.log('handleClose')
      this.$emit('update:open', false)
    },
    // 获取高低报
    // 获取列表
    getList() {
      suppliesRealinfoWarning(this.query).then((res) => {
        res.records.forEach((item) => {
          item.realInfo = JSON.parse(item.realInfo)
        })
        this.tableData = res.records
        this.total = res.total
      })
    },
  },
  mounted() {
    this.getList()
  },
}
</script>

<style lang="scss" scoped>
@import '~@/assets/scss/warn_dialog.scss';
</style>

<style lang="scss">
.warn_dialog_proper {
}
</style>

